function ShowToast(message) {
    uni.showToast({
        title: message,
        icon: 'none',
        duration: 1000,
        mask: true
    });
}
function ShowModel(content) {
    uni.showModal({
        content: content,
        showCancel: false
    });
}
//验证手机号格式
function IsPoneAvailable(poneInput) {
    var myreg = /^[1][3,4,5,6,7,8][0-9]{9}$/;
    if (!myreg.test(poneInput)) {
        return false;
    } else {
        return true;
    }
}
//获取当前日期
function GetCurrentDate() {
    var formatDate = '';
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    return (formatDate = year + '-' + month + '-' + day);
}
//获取前n个月时间;n<12
function GetPreDate(date, n) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var day = arr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2 = parseInt(month) - n;
    if (month2 <= 0) {
        year2 = parseInt(year2) - 1;
        month2 = parseInt(month) - n + 12;
    }
    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
}
//获取前n个天时间
function GetPreDateDay(num) {
    var date1 = new Date();
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() + num);
    var time2 = addZero(date2.getFullYear()) + '-' + addZero(date2.getMonth() + 1) + '-' + addZero(date2.getDate());
    return time2;
}
//根据当前天获取前n个天时间
function GetPreDateByDay(date, num) {
    var date1 = new Date(date);
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() + num);
    var time2 = addZero(date2.getFullYear()) + '-' + addZero(date2.getMonth() + 1) + '-' + addZero(date2.getDate());
    return time2;
}
function addZero(num) {
    //补0
    if (parseInt(num) < 10) {
        num = '0' + num;
    }
    return num;
}
//获取当前时间戳
function Timestamp() {
    var timestamp = new Date().getTime();
    return timestamp;
}
//转换为时间戳
function ToTimestamp(val) {
    var date = val;
    var _date = new Date(Date.parse(date.replace(/-/g, '/')));
    return _date.getTime();
}
//转换为时间戳到秒
function ToTimestampS(val) {
    let thisTime = val;
    thisTime = thisTime.replace(/-/g, '/');
    let time = new Date(thisTime);
    return time.getTime();
}
// 获取当前时间
function getNowTime() {
    var date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    if (month < 10) {
        month = '0' + month;
    }
    let day = date.getDate();
    if (day < 10) {
        day = '0' + day;
    }
    let hour = date.getHours() > 9 ? date.getHours().toString() : '0' + date.getHours();
    let min = date.getMinutes() > 9 ? date.getMinutes().toString() : '0' + date.getMinutes();
    // let sec = "00";
    let sec = date.getSeconds() > 9 ? date.getSeconds().toString() : '0' + date.getSeconds();
    let weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    let weekDay = weekDays[date.getDay()]; // 周几
    // 年-月-日
    let nowDate = year + '-' + month + '-' + day;
    // 年-月-日 时:分:秒
    let nowDateTime = year + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec;
    let nowDateMin = year + '-' + month + '-' + day + ' ' + hour + ':' + min;
    let timeObj = {
        weekDay,
        nowDate,
        nowDateTime,
        nowDateMin
    };
    return timeObj;
}

//获取IP地址
function GetHttpsIP(success_callback) {
    uni.request({
        url: 'https://pv.sohu.com/cityjson?ie=utf-8',
        success: function (resultpack) {
            success_callback(resultpack);
        },
        fail: function () {
            //错误响应处理
            var resultpack = {
                data: 'var returnCitySN = {"cip": "127.0.0.1", "cid": "320100", "cname": "江苏省南京市"};'
            };
            success_callback(resultpack);
        }
    });
}
//表情解码
function UncodeUtf16(str) {
    var reg = /\&#.*?;/g;
    var result = str.replace(reg, function (char) {
        var H;
        var L;
        var code;
        if (char.length == 9) {
            code = parseInt(char.match(/[0-9]+/g));
            H = Math.floor((code - 65536) / 1024) + 55296;
            L = ((code - 65536) % 1024) + 56320;
            return unescape('%u' + H.toString(16) + '%u' + L.toString(16));
        } else {
            return char;
        }
    });
    return result;
}
//表情转码
function Utf16toEntities(str) {
    var patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
    str = str.replace(patt, function (char) {
        var H;
        var L;
        var code;
        if (char.length === 2) {
            H = char.charCodeAt(0); // 取出高位
            L = char.charCodeAt(1); // 取出低位
            code = (H - 55296) * 1024 + 65536 + L - 56320; // 转换算法
            return '&#' + code + ';';
        } else {
            return char;
        }
    });
    return str;
}
/*获取ReturnUrl*/
function GetReturnUrl(sort) {
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - sort];
    var url = currentPage.route;
    var options = currentPage.options;
    if (JSON.stringify(options) != undefined && JSON.stringify(options) != '{}') {
        var urlWithArgs = url + '?';
        for (var key in options) {
            var value = options[key];
            urlWithArgs += key + '=' + value + '&';
        }
        urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
        return urlWithArgs;
    } else {
        return url;
    }
}

//页面跳转
function GoUrl(Url, JumpMode) {
    if (Url != '') {
        if (JumpMode == 'reLaunch') {
            uni.reLaunch({
                url: Url
            });
        }
        if (JumpMode == 'navigateTo') {
            uni.navigateTo({
                url: Url
            });
        }
        if (JumpMode == 'switchTab') {
            uni.switchTab({
                url: Url
            });
        }
        if (JumpMode == 'redirectTo') {
            uni.redirectTo({
                url: Url
            });
        }
    } else {
        ShowToast('敬请期待');
    }
}
//把时间戳转化成天时分秒格式
function GetTime(timeStamp) {
    var day = 0;
    var hours = 0;
    var minute = 0;
    var seconds = 0;
    day = Math.floor(timeStamp / 1000 / 60 / 60 / 24); //计算天数
    hours = Math.floor((timeStamp / 1000 / 60 / 60) % 24);
    minute = Math.floor((timeStamp / 1000 / 60) % 60);
    seconds = Math.floor((timeStamp / 1000) % 60);
    var h = hours < 10 ? '0' + hours : hours;
    var m = minute < 10 ? '0' + minute : minute;
    var s = seconds < 10 ? '0' + seconds : seconds;
    var rest_time = {
        day: day,
        hours: h,
        minutes: m,
        seconds: s
    };
    return rest_time;
}
//选择后一天
function GetnextDate() {
    var formatDate = '';
    var curDate = new Date();
    var nextdata = new Date(curDate.getTime() + 86400 * 1000);
    //获取当前日期
    var year = nextdata.getFullYear();
    var month = nextdata.getMonth() + 1;
    var day = nextdata.getDate();
    if (month < 10) {
        month = '0' + month;
    }
    if (day < 10) {
        day = '0' + day;
    }
    return (formatDate = year + '-' + month + '-' + day);
}
//返回上一页
function Backpage() {
    var pages = getCurrentPages();
    if (pages.length == 1) {
        uni.reLaunch({
            url: '/pages/index/index'
        });
    } else {
        var prevPage = pages[pages.length - 2];
        uni.navigateBack({
            //返回
            delta: 1
        });
    }
}
//校验身份证号合法性
function isCardID(sId) {
    var aCity = {
        11: '北京',
        12: '天津',
        13: '河北',
        14: '山西',
        15: '内蒙古',
        21: '辽宁',
        22: '吉林',
        23: '黑龙江',
        31: '上海',
        32: '江苏',
        33: '浙江',
        34: '安徽',
        35: '福建',
        36: '江西',
        37: '山东',
        41: '河南',
        42: '湖北',
        43: '湖南',
        44: '广东',
        45: '广西',
        46: '海南',
        50: '重庆',
        51: '四川',
        52: '贵州',
        53: '云南',
        54: '西藏',
        61: '陕西',
        62: '甘肃',
        63: '青海',
        64: '宁夏',
        65: '新疆',
        71: '台湾',
        81: '香港',
        82: '澳门',
        91: '国外'
    };
    var iSum = 0;
    if (!/^\d{17}(\d|x)$/i.test(sId)) {
        return '你输入的身份证长度或格式错误';
    }
    sId = sId.replace(/x$/i, 'a');
    if (aCity[parseInt(sId.substr(0, 2))] == null) {
        return '你的身份证地区非法';
    }
    var sBirthday = sId.substr(6, 4) + '-' + Number(sId.substr(10, 2)) + '-' + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, '/'));
    if (sBirthday != d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()) {
        return '身份证上的出生日期非法';
    }
    for (var i = 17; i >= 0; i--) {
        iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
    }
    if (iSum % 11 != 1) {
        return '你输入的身份证号非法';
    }
    //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
    return true;
}
module.exports = {
    ShowToast: ShowToast,
    ShowModel: ShowModel,
    IsPoneAvailable: IsPoneAvailable,
    GetCurrentDate: GetCurrentDate,
    GetPreDate: GetPreDate,
    GetHttpsIP: GetHttpsIP,
    Timestamp: Timestamp,
    ToTimestamp: ToTimestamp,
    getNowTime: getNowTime,
    UncodeUtf16: UncodeUtf16,
    Utf16toEntities: Utf16toEntities,
    GetReturnUrl: GetReturnUrl,
    GoUrl: GoUrl,
    ToTimestampS: ToTimestampS,
    GetTime: GetTime,
    GetnextDate: GetnextDate,
    Backpage: Backpage,
    GetPreDateDay: GetPreDateDay,
    GetPreDateByDay: GetPreDateByDay,
    isCardID: isCardID
};
